package com.jieshuibao.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.jieshuibao.mapper.QuestionMapper;
import com.jieshuibao.mapper.QuestionUserMapper;
import com.jieshuibao.mapper.SecretlyExMapper;
import com.jieshuibao.mapper.SecretlyMapper;
import com.jieshuibao.model.db.Question;
import com.jieshuibao.model.db.Secretly;
import com.jieshuibao.model.db.SecretlyExample;
import com.jieshuibao.util.PayTypeUtil;
import com.jieshuibao.wx.util.PayCommonUtil;

@Service
public class SecretlyService{
	
	@Resource
	private SecretlyMapper secretlyMapper;
	
	@Resource
	private SecretlyExMapper secretlyExMapper;
	
	@Resource
	private QuestionMapper questionMapper;
	
	@Resource
	private QuestionUserMapper questionUserMapper;
	
	@Resource
	private UserDealRecordService userDealRecordService;
	
	public void addSecretly(Secretly secretly) {
		secretlyMapper.insertSelective(secretly);
	}
	
	public void delSecretlyByUuid(String uuid) {
		secretlyMapper.deleteByPrimaryKey(uuid);
	}
	
	public void updateSecretly(Secretly secretly) {
		secretlyMapper.updateByPrimaryKeySelective(secretly);
	}
	
	public Secretly getSecretlyByUuid(String uuid) {
		return secretlyMapper.selectByPrimaryKey(uuid);
	}
	
	public List<Secretly> getPageSecretly(SecretlyExample secretlyExample) {
		return secretlyMapper.selectPage(secretlyExample);
	}
	
	public int countSecretly(SecretlyExample secretlyExample) {
		return secretlyMapper.countByExample(secretlyExample);
	}
	
	public double getSumMoneyUser(String phone,String id){
		Double moneyUser = secretlyExMapper.selectTatilMoneyUser(phone, id);
		if(moneyUser==null){
			return 0;
		}else {
			return moneyUser;
		}
	}
	
	public boolean buyQuestion(String uuid,String uuid2,String userId,String paytype,double money){
		
		Secretly secretly = new Secretly();
		//String uuid2 = IdGen.uuid();
		secretly.setUuid(uuid2);
		
		secretly.setQuestionUuid(uuid);
		Question question = questionMapper.selectByPrimaryKey(uuid);
		if (question==null) {
			//id错误
			return false;
		}
		Date date = new Date();
		
		Integer status = question.getStatus();
		if (status!=null&&status==3) {
			String phoneNum = question.getPhoneNum();
			secretly.setMentionPhoneNum(phoneNum);
			
			secretly.setPayType(paytype);
			secretly.setPeepPhoneNum(userId);
			secretly.setSecretlyDate(date);
			secretly.setSecretlyMoney(money);
			secretly.setMoney(money);
			
			if (paytype.equals("balance")) {
				secretly.setPayType(PayTypeUtil.PAYTYPE_BALANCE);
				secretly.setPayStatus(PayCommonUtil.PAYSUCCESS_STATUS);
				
				
			}else if (paytype.equals("VipBalance")) {
				secretly.setPayType(PayTypeUtil.PAYTYPE_VIPBALANCE);
				secretly.setPayStatus(PayCommonUtil.PAYSUCCESS_STATUS);
							}else if (paytype.equals("weixin")) {
				secretly.setPayType(PayTypeUtil.PAYTYPE_WEIXIN);
				secretly.setPayStatus(PayCommonUtil.PAYPENDING_STATUS);
			}
			secretlyMapper.insertSelective(secretly);
			if (paytype.equals("balance")) {
				userDealRecordService.onLookRunning(userId, uuid2, PayTypeUtil.PAYTYPE_BALANCE, money);
			}else if (paytype.equals("VipBalance")) {
				userDealRecordService.onLookRunning(userId, uuid2,PayTypeUtil.PAYTYPE_VIPBALANCE, money);

			}
			
			
			return true;
		}else {
			//状态错误
			return false;
		}
		
	}
}
